/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui;

import app.entities.model.DTO.MatriculaAlunoDTO;
import app.entities.model.DTO.NotasAlunoDTO;
import app.view.classes.ViewAluno;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class VerNotas extends javax.swing.JFrame {
    
    private ViewAluno viewAluno;
    private MatriculaAlunoDTO matriculaAluno;
    private ArrayList<NotasAlunoDTO> listaNotas;

    public VerNotas() {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width/2) - (getWidth()/2), (screenSize.height/2) - (getHeight()/2));
        this.viewAluno = new ViewAluno();
        this.matriculaAluno = new MatriculaAlunoDTO();
        this.listaNotas = new ArrayList<>();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        edtCodigoMatricula = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnSelecionarMatricula = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        edtNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        edtSobrenome = new javax.swing.JTextField();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        cbTurmas = new javax.swing.JComboBox();
        btnVerNotas = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNotas = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Notas");

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Matrícula"));

        edtCodigoMatricula.setEnabled(false);
        edtCodigoMatricula.setBounds(120, 20, 250, 20);
        jLayeredPane1.add(edtCodigoMatricula, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setText("Código da Matrícula:");
        jLabel1.setBounds(10, 20, 120, 14);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnSelecionarMatricula.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/conultar32x32.png"))); // NOI18N
        btnSelecionarMatricula.setText("Selecionar Aluno");
        btnSelecionarMatricula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelecionarMatriculaActionPerformed(evt);
            }
        });
        btnSelecionarMatricula.setBounds(120, 130, 170, 41);
        jLayeredPane1.add(btnSelecionarMatricula, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Nome:");
        jLabel2.setBounds(10, 60, 31, 14);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        edtNome.setEnabled(false);
        edtNome.setBounds(120, 60, 250, 20);
        jLayeredPane1.add(edtNome, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Sobrenome:");
        jLabel3.setBounds(10, 100, 80, 14);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        edtSobrenome.setEnabled(false);
        edtSobrenome.setBounds(120, 100, 250, 20);
        jLayeredPane1.add(edtSobrenome, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Turmas Matriculadas"));

        cbTurmas.setBounds(10, 20, 360, 20);
        jLayeredPane2.add(cbTurmas, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnVerNotas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/vernotas32x32.png"))); // NOI18N
        btnVerNotas.setText("Ver Notas");
        btnVerNotas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerNotasActionPerformed(evt);
            }
        });
        btnVerNotas.setBounds(120, 50, 170, 41);
        jLayeredPane2.add(btnVerNotas, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Notas"));

        tblNotas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Bimestre 1", "Bimestre 2", "Nota Final"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblNotas);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1)
            .addComponent(jLayeredPane2, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVerNotasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerNotasActionPerformed
        if (cbTurmas.getItemCount() == 0){
            JOptionPane.showMessageDialog(this, "Atenção! Você não tem notas lançadas nessa matéria.",
                        "Ver Notas", JOptionPane.INFORMATION_MESSAGE);
            return;
        } else if(cbTurmas.getModel().getSelectedItem().toString().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Atenção! Selecione uma turma.", "Turma", JOptionPane.WARNING_MESSAGE);
            return;   
        }else {
            DefaultTableModel model = (DefaultTableModel) this.tblNotas.getModel();
            this.listaNotas = this.viewAluno.pesquisarNotasAluno(this.matriculaAluno.getCodigoMatricula(),
                    cbTurmas.getSelectedItem().toString());
            if(this.listaNotas.size() == 2){
            model.addRow(new Object[]{this.listaNotas.get(0).getValorNota(), this.listaNotas.get(1).getValorNota(),
                this.viewAluno.calcularNotaAluno(this.listaNotas.get(0).getValorNota(), this.listaNotas.get(1).getValorNota())});
            }
            else if(this.listaNotas.size() == 1){
                model.addRow(new Object[]{this.listaNotas.get(0).getValorNota(), 0, 
                    this.viewAluno.calcularNotaAluno(this.listaNotas.get(0).getValorNota(), 0)});
            }
            else{
                JOptionPane.showMessageDialog(this, "Atenção! Você não tem notas lançadas nessa matéria.",
                        "Ver Notas", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnVerNotasActionPerformed

    private void btnSelecionarMatriculaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelecionarMatriculaActionPerformed
        DefaultComboBoxModel cbModel = (DefaultComboBoxModel) this.cbTurmas.getModel();
        cbModel.removeAllElements();
        ListaAlunosMatriculados LAM = new ListaAlunosMatriculados(this.matriculaAluno, this.edtCodigoMatricula,
                this.edtNome, this.edtSobrenome, this.cbTurmas);
        LAM.setVisible(true);
    }//GEN-LAST:event_btnSelecionarMatriculaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSelecionarMatricula;
    private javax.swing.JButton btnVerNotas;
    private javax.swing.JComboBox cbTurmas;
    private javax.swing.JTextField edtCodigoMatricula;
    private javax.swing.JTextField edtNome;
    private javax.swing.JTextField edtSobrenome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblNotas;
    // End of variables declaration//GEN-END:variables
}
